package com.example.konwRice.adapter;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.konwRice.Dao.HistoryDao;
import com.example.konwRice.R;
import com.example.konwRice.domain.item;

import java.util.List;

public class item_adapter extends ArrayAdapter<item> {

    private int resourceId;
    private HistoryDao historyDao;

    public item_adapter(Context context, int textViewResourceId, List<item> objects) {
        super(context, textViewResourceId, objects);
        resourceId = textViewResourceId;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        item new_item = getItem(position);
        View view = LayoutInflater.from(getContext()).inflate(resourceId, parent, false);

        ImageView newImage = (ImageView) view.findViewById(R.id.new_image);
        TextView newTitle = (TextView) view.findViewById(R.id.new_title);

        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Intent.ACTION_VIEW);
                intent.setData(Uri.parse(new_item.getUrl()));
                view.getContext().startActivity(intent);

                historyDao=new HistoryDao(view.getContext());
                historyDao.insertHistory(new_item);

            }
        });
        newImage.setImageResource(new_item.getImageId());
        newTitle.setText(new_item.getTitle());


        return view;
    }
}
